package com.neusoft.elmboot.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.neusoft.elmboot.mapper.PointLogMapper;
import com.neusoft.elmboot.mapper.PointMapper;
import com.neusoft.elmboot.po.Point;
import com.neusoft.elmboot.po.PointLog;
import com.neusoft.elmboot.service.PointService;

@Service
public class PointServiceImpl implements PointService{
    
    @Autowired
    private PointMapper pointMapper;

    @Autowired
    private PointLogMapper pointLogMapper;


    @Autowired
    private PointLogServiceImpl pointLogService;
    
//    @Value("${points.payment.rate}")
//    private int pointsPaymentRate;
//
//    @Value("${points.exchange.rate}")
//    private int pointsExchangeRate;

    @Override
    public int savePoint(String userId) {
        PointLog pointLog= new PointLog();
        pointLog.setUserId(userId);
        pointLogService.savePointLog(pointLog,new BigDecimal(10));
        return pointMapper.savePoint(userId);
    }

    @Override
    public Point getPointByUserid(String userId) {
        return pointMapper.getPointByUserid(userId);
    }

    @Override
    public int updatePointCount(String userId, BigDecimal count) {
        return pointMapper.updatePointCount(userId, count);
    }

    //使用积分
    @Override
    public int usePoint(String userId, BigDecimal count) {
        Point point= pointMapper.getPointByUserid(userId);
        //计算使用抵扣金额的积分
        BigDecimal amount = count.multiply(new BigDecimal(100)).setScale(0, RoundingMode.DOWN);
        System.out.println(amount);
        point.subCount(amount);
        System.out.println(point.getCountValue());
        pointLogMapper.updatePointLogByPay(userId, amount);
        return pointMapper.updatePointCount(userId, point.getCountValue());
    }

    //增加积分（将来根据不同的业务逻辑进行处理，并且计算支付金额获得的积分）
    @Override
    public void addPoints(String userId, BigDecimal acount,String type) {
        if ("payment".equals(type)) {
            // 计算付款获得的积分
            BigDecimal amount = acount.multiply(new BigDecimal(10));
            Point point= pointMapper.getPointByUserid(userId);
            point.addCount(amount);
            pointMapper.updatePointCount(userId, point.getCountValue());
            PointLog pointLog= new PointLog();
            pointLog.setUserId(userId);
            pointLogService.savePointLog(pointLog,amount);
        } else if ("comment".equals(type)) {
            // 评价固定获得的积分，这里假设为50分
            Point point= pointMapper.getPointByUserid(userId);
            point.addCount(new BigDecimal(50));
            pointMapper.updatePointCount(userId, point.getCountValue());
            PointLog pointLog= new PointLog();
            pointLog.setUserId(userId);
            pointLogService.savePointLog(pointLog,new BigDecimal(50));
            
        }
    }


    
    
}
